layui.use(['table', 'admin', 'ax','laydate','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

    laydate.render({
        elem: '#startTime'
        ,value:new Date()
    });

    laydate.render({
        elem: '#endTime'
        ,value:new Date()
    });

    /**
     * 调度单表管理
     */
    var carrierRecFee = {
        tableId: "carrierRecFeeTable"
    };

    /**
     * 初始化表格的列
     */
    carrierRecFee.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'operateName', title: '项目', style:'font-weight: 600;'},
            {field: 'trainNumber', title: '车次', style:'font-weight: 600;', sort: true},
            {field: 'startDate', title: '发车日期', style:'font-weight: 600;', sort: true},
            {field: 'fayunTimes', title: '发运时间', sort: true},
            {field: 'dispatchNumber', title: '运输单号', style:'font-weight: 600;'},
            {field: 'carnumber', title: '车牌号', style:'font-weight: 600;', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'driver1', title: '司机', style:'font-weight: 600;', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'tel1', title: '联系方式', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'privatecarType', title: '车型', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'mileageTraveled', title: '行驶里程', style:'background-color: #dbf1e1;font-weight: 600;', sort: true},
            {field: 'loadingType', title: '装车方', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'loadingPersonnel', title: '装车人员', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'privatecarSurevolume', title: '准载方数m3', style:'background-color: #dbf1e1;font-weight: 600;', sort: true},
            {field: 'toovolume', title: '总方数m3', style:'font-weight: 600;', totalRow: true, sort: true},
            {field: 'carrierName', sort: true, title: '承运商', style:'font-weight: 600;'},
            {field: 'lineName', sort: true, title: '线路', style:'font-weight: 600;'},
            {field: 'consultMoney', title: '参考运费￥', style:'font-weight: 600;',totalRow: true,templet: '#consultMoneyTpl'},
            {field: 'freightMoney', title: '运费￥', style:'font-weight: 600;',totalRow: true},
            {field: 'loadingMoney', sort: true, title: '装车费￥', style:'font-weight: 600;', totalRow: true},
            {field: 'xiechefei', sort: true, title: '卸车费￥', style:'font-weight: 600;', totalRow: true},
            // {align: 'center', toolbar: '#tableBar', title: '操作', width:90, fixed: 'right'},
        ]];
    };

    /**
     * 导出excel按钮
     */
    carrierRecFee.exportExcel = function () {
        var checkRows = table.checkStatus(carrierRecFee.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
            var queryData = {};
            queryData['operateId'] = $("#operateId").val();
            queryData['condition'] = $("#condition").val();
            queryData['startTime'] = $("#startTime").val();
            queryData['endTime'] = $("#endTime").val();
            queryData['operateName'] = $("#operateName").val();
            queryData['carrierName'] = $("#carrierName").val();
            queryData['carrierId'] = $("#carrierId").val();
            queryData['transportNumber'] = $("#transportNumber").val();
            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/rec/carrierRecFee/listAll",
                dataType:'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });
        }
    };

    carrierRecFee.btnJsyf = function () {
        var checkRows = table.checkStatus(carrierRecFee.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要计算费用的数据");
        } else {
            var ids = checkRows.data.map(function(item) {
                return item.id;
            });
        console.log(ids);
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/rec/carrierRecFee/jsfy", function (data) {
                    if(data.success){
                        Feng.success("操作成功!");
                        table.reload(carrierRecFee.tableId);
                    }else{
                        Feng.error("操作失败!" + data.message + "!");
                    }
                }, function (data) {
                    Feng.error("操作失败!" + data.responseJSON.message + "!");
                });
                ajax.set("ids", ids.join(","));
                ajax.start();
            };
            Feng.confirm("是否对选择的记录发起计算费用?", operation);
        }
    };

    // 导出excel
    $('#btnExp').click(function () {
        carrierRecFee.exportExcel();
    });

    // 导出excel
    // $('#btnJsyf').click(function () {
    //     carrierRecFee.btnJsyf();
    // });
    /**
     * 点击查询按钮
     */
    carrierRecFee.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId").val();
        queryData['condition'] = $("#condition").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['operateName'] = $("#operateName").val();
        queryData['carrierName'] = $("#carrierName").val();
        queryData['carrierId'] = $("#carrierId").val();
        queryData['transportNumber'] = $("#transportNumber").val();
        table.reload(carrierRecFee.tableId, {
            where: queryData, page: {curr: 1},
            done: function (res, curr, count) {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + carrierRecFee.tableId,
        url: Feng.ctxPath + "/rec/carrierRecFee/listPage",
        page: true,
        limit: 60,
        limits:[60,120,240,360,480,1500,5000],
        height: "full-60",
        // totalRow: true,
        // toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        defaultToolbar: ['filter', 'exports', 'print'],
        cellMinWidth: 100,
        cols: carrierRecFee.initColumn(),
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        carrierRecFee.search();
    });

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    carrierRecFee.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/rec/carrierRecFee/edit?id=' + data.id;
    };

    // 单击行事件
    table.on('row(' + carrierRecFee.tableId + ')', function (obj) {
        var data = obj.data;
        $('div[lay-id="' + carrierRecFee.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
        $('div[lay-id="' + carrierRecFee.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });

    // 工具条点击事件
    table.on('tool(' + carrierRecFee.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'tooDetailed') {
            layer.open({
                type: 2,
                title: '配送派车信息',
                area: ['1250px', '570px'],
                anim: 5,
                content: Feng.ctxPath + '/tDispatch/tooDetailed?dispatchNumber=' + data.dispatchNumber,
            });
        } else if (layEvent === 'tooConsultMoneyDetailed') {
            layer.open({
                type: 2,
                title: '费用信息',
                area: ['1100px', '500px'],
                anim: 5,
                content: Feng.ctxPath + '/transportCost?dispatchNumber=' + data.dispatchNumber,
            });
        }else if (layEvent === 'edit') {
            layer.open({
                type: 2,
                title: '修改运费',
                area: ['1100px', '500px'],
                anim: 5,
                content: Feng.ctxPath + '/rec/carrierRecFee/edit?id=' + data.id,
                end: function () {
                    table.reload(carrierRecFee.tableId);
                }
            });
        }
    });

    //查询库区
    form.on('select(operateId)',function(){
        $("#carrierId").empty();
        $.ajax({
            url:Feng.ctxPath + '/basicCarrier/Querylist',
            dataType:"json",   //返回格式为json
            async: false,
            data:{"operateId":$("#operateId").val()},    //参数值
            type:"post",   //请求方式
            success:function(data){
                if(data.length>0){
                    var str = "<option value=''>选择承运商</option>";
                    $.each(data,function(key,value){
                        str += "<option value='"+value.id+"'>"+value.carrierName+"</option>";
                    });
                    $("#carrierId").html(str);
                }
            }
        });
        form.render('select');
    });

});
